<?php

include_once dirname(__FILE__) . "/error.php";
include_once dirname(__FILE__) . "/log.php";
include_once dirname(__FILE__) . "/db.php";
include_once dirname(__FILE__) . "/open.php";

class Inf
{
    private $db = null;
    private $open = null;

    public function __construct()
    {
        $this->db = new Db();
        $ret = $this->db->init();
        if($ret == false)
        {
            Log::error("setup db failed");
            $this->db = null;
        }
        $this->open = new Open();
        $ret = $this->open->init();
        if($ret == false)
        {
            Log::error("setup open qq failed");
            $this->open = null;
        }
    }
    
    public function serv($arg)
    {
        global $err_code;
        if(!is_array($arg) || !array_key_exists("interface", $arg))
        {
            $err_code = ec_post_error;
            Log::error("post data invalid");
            return $this->ret_msg();
        }
        
        $inf = strtolower((string)$arg['interface']);
        switch($inf)
        {
        case "get_dish_list":
            $ret = $this->get_dish_list($arg);
            break;
       
        case "get_recommend_accounts":
            $ret = $this->get_recommend_accounts($arg);
            break;
        
        case "get_recommend_restaurants":
            $ret = $this->get_recommend_restaurants($arg);
            break;
        
        default:
            $err_code = ec_inf_error;
            Log::error("invalid interface:" . $inf);
            return $this->ret_msg();
        }

       return $this->ret_msg($ret); 
    }

    public function get_dish_list($args)
    {
        global $err_code;
        
        $home = $this->open->get_info($args);
        if($home == false || (is_array($home) && $home['code'] != 0))
        {
            $err_code = ec_open_qq_error;
            Log::error("get_info failed.");
            return false;
        }
     
        $new_user = $this->db->is_new_user($args);
        if($new_user === false)
        {
            Log::error("is_new_user failed.");
            return false;
        }
        else if(count($new_user) == 0)
        {
            $top_dish = $this->db->get_top_dish();
            Log::debug("top_dish:" . var_export($top_dish, true));
            if($top_dish === false)
                return false;

            $home_dish = $this->db->get_home_dish($home);
            Log::debug("home_dish:" . var_export($home_dish, true));
            if($home_dish === false)
                return false;

            $selected = array_merge($top_dish, $home_dish);
            if($selected === false)
                return false;

            $rand_dish = $this->db->get_random_dish($selected);
            Log::debug("rand_dish:" . var_export($rand_dish, true));
            if($rand_dish === false)
                return false;

            $ret = array_merge($top_dish, $home_dish, $rand_dish);
            Log::debug("ret:" . var_export($ret, true));
            if($ret == false)
                return false;
        }
        else
        {
            $eaten_dish = $this->db->get_eaten_dish($args);
            Log::debug("eaten_dish:" . var_export($eaten_dish, true));
            if($eaten_dish === false)
                return false;
            $friend_dish = $this->db->get_friend_dish($args);
            Log::debug("friend_dish:" . var_export($friend_dish, true));
            if($friend_dish === false)
                return false;
            $selected = array_merge($eaten_dish, $friend_dish);
            if($selected === false)
                return false;
            $rand_list = $this->db->get_random_dish($selected);
            Log::debug("rand_list:" . var_export($rand_list, true));
            if($rand_list === false)
                return false;
            $ret = array_merge($eaten_dish, $friend_dish, $rand_list);
            Log::debug("ret:" . var_export($ret, true));
            if($ret == false)
                return false;
        }

        return $ret;
    }

    public function get_recommend_accounts($args)
    {
        global $err_code;

        $ret = $this->db->update_dish_vote($args);
        Log::debug("update dish vote:" . var_export($ret, true));
        if($ret == false)
            return false;

        $family = $this->db->get_similar_dish_family_user($args);
        Log::debug("similar family:" . var_export($family, true));
        if($family === false)
            return false;

        $dish = $this->db->get_similar_dish_user($args);
        Log::debug("similar dish:" . var_export($dish, true));
        if($dish === false)
            return false;

        $list = array_intersect($dish, $family);
        Log::debug("intersect:" . var_export($list, true));
        if($list === false)
            return false;
        
        $new_list = $this->db->float_once_buddy($list, $args);
        if(is_array($new_list) && count($new_list) == 0)
        {
            $new_list = $this->db->get_random_user($args);
        }
        Log::debug("new_list:" . var_export($new_list, true));
        if($new_list === false)
            return false;
        
        $new_list = $this->db->add_similar_tag($args, $new_list);
        if($new_list === false)
            return false;

        return $new_list;
    }
    
    public function get_recommend_restaurants($args)
    {
       // 1. get avg vote
       $avg = $this->db->get_rest_vote($args); 
       Log::debug("avg:" . var_export($avg, true));
       
       // 2. calc current vote ratio
       $num = count($avg);
       $pred_votes = array();
       foreach($avg as $rest_a)
       {
           $sum = 0;
           $rest_id = $rest_a['rest_id'];
           $user_vote = $this->db->get_user_vote($args, $rest_id);
           foreach($avg as $rest_b)
           {
               if($rest_b['rest_id'] == $rest_a['rest_id'])
               {
                   continue;
               }
               $delta = $rest_b['rest_vote'] - $rest_a['rest_vote'];
               $sum += $delta;
           }
           $pred_vote = $user_vote +  $sum / $num;
           $one = array(
               $rest_a['rest_id'] => $pred_vote,
           );
           $pred_votes = array_merge($pred_votes, $one);
       }

       asort($pred_votes, SORT_NUMERIC);
       $all = array();
       foreach($pred_votes as  $idx => $value)
       {
           array_push($all, $avg[$idx]);
       }
       $all = array_unique($all, SORT_REGULAR);
       Log::debug("all:" . var_export($all, true));
       return $all;
    }
    
    public function ret_msg($data = array())
    {
        global $err_code, $err_msg;
        return array(
            "code" => $err_code, 
            "msg" => $err_msg[$err_code],
            "data" => $data,
        );
    }
};

?>
